package string;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : StringDemo
 * @Author : 54061
 * @Date: 2022/2/14 15:03
 */
public class StringDemo {
    public static void main(String[] args) throws UnsupportedEncodingException, UnsupportedEncodingException {
        //UTF-8编码格式下  一个中文占用3个字节
        //英文大小写,数字,英文符号都占用1个字节
        String line = "我爱你China";

        //编码 ->
        byte[] bytes1 = line.getBytes();//按照平台默认的编码格式进行编码 UTF-8
        System.out.println(Arrays.toString(bytes1));

        ////UTF-8编码格式下  一个中文占用2个字节
        byte[] bytes2 = line.getBytes("GBK");//按照指定的编码格式进行编码 GBK
        System.out.println(Arrays.toString(bytes2));

        //解码 ->
        String s1 = new String(bytes1);
        System.out.println("s1 = " + s1);//我爱你China
        String s2 = new String(bytes2);
        System.out.println("s2 = " + s2);//乱的

        String s4 = new String(bytes1,"GBK");
        System.out.println("s4 = " + s4);//乱的
        String s5 = new String(bytes2,"GBK");
        System.out.println("s5 = " + s5);//不乱



    }
}
